package cz.vargagab.repo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;
import org.springframework.stereotype.Repository;

import cz.vargagab.domain.ClientRmiNode;
import cz.vargagab.domain.LogObject;
import cz.vargagab.domain.NumberWrapper;
import cz.vargagab.domain.Request;
import cz.vargagab.domain.ServerRmiNode;

public class InternalClientServiceImpl implements InternalClientService {

	@Autowired
	private ClientRmiNode thisNode;
	
	private List<LogObject> log = new ArrayList<LogObject>();
	
	private Queue<Request> queue = new PriorityQueue<Request>(); //prioritna fronta, radenie podla timestampov, lebo Request ma metodu compareTo
	private Map<Request,Map<ClientRmiNode,Boolean>> replys = new HashMap<Request,Map<ClientRmiNode,Boolean>>(); //mapa pre kazdy request mam zoznam svojich uzlov, a k nim informaciu, ci som som dostal reply
	
	private List<ClientRmiNode> clientNodes;//ostatne nody
	private boolean loggedIn = false;
	private ServerRmiNode serverNode;
	private NumberWrapper numberWrap = new NumberWrapper(1); //lokalna instancia zdielanej premnenej
	private Map<ClientRmiNode,ExternalClientService> clients = new HashMap<ClientRmiNode,ExternalClientService>(); //ostatne nody a interface na nich
	
	public ServerRmiNode getServerNode() {
		return serverNode;
	}

	public void setServerNode(ServerRmiNode serverNode) {
		this.serverNode = serverNode;
	}
	
	public List<ClientRmiNode> getClientNodes() {
		return clientNodes;
	}

	// dostanem sem od servru vsetkych klientov vratane seba - to ale nechcem, preto seba vylucim
	public void setClientNodes(List<ClientRmiNode> clientNodes) {
		this.clientNodes = clientNodes;
		this.clientNodes.remove(thisNode); //vylucim seba
		
		clients = new HashMap<ClientRmiNode,ExternalClientService>();
		
		//pre kazdeho klienta(uz okrem seba) vytvorim odkaz na jeho RMI interface
		for (ClientRmiNode node: clientNodes){
			RmiProxyFactoryBean temp = new RmiProxyFactoryBean();
			temp.setServiceInterface(ExternalClientService.class);
			temp.setServiceUrl("rmi://"+node.getIpAddress()+":1099/"+node.getClientToServiceIface());
			temp.afterPropertiesSet();
			ExternalClientService service = (ExternalClientService) temp.getObject();
			clients.put(node, service);
		}
	}

	public NumberWrapper getNumberWrap() {
		return numberWrap;
	}
	public void setNumberWrap(NumberWrapper numberWrap) {
		this.numberWrap = numberWrap;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}
	public void setLoggedIn(boolean loggedIn) {
		this.loggedIn = loggedIn;
	}

	public Map<ClientRmiNode, ExternalClientService> getClients() {
		return clients;
	}
	
	public ClientRmiNode getThisNode() {
		return thisNode;
	}
	public void setThisNode(ClientRmiNode thisNode) {
		this.thisNode = thisNode;
	}

	public Queue<Request> getQueue() {
		return queue;
	}
	public void setQueue(Queue<Request> queue) {
		this.queue = queue;
	}

	public Map<Request, Map<ClientRmiNode, Boolean>> getReplys() {
		return replys;
	}
	public void setReplys(Map<Request, Map<ClientRmiNode, Boolean>> replys) {
		this.replys = replys;
	}

	public List<LogObject> getLog() {
		return log;
	}
	public void setLog(List<LogObject> log) {
		this.log = log;
	}
	
}
